package com.example.demo.controllerPage;

import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;
import com.example.demo.annotation.IgnoreResponseAdvice;
import com.example.demo.entity.UserInfoTest;
import com.example.demo.service.UserInfoTestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.Optional;

/**
 * @Author: Pinp
 * @Date: 2021/7/21 2:21
 */

@Slf4j
@Controller
@RequestMapping("/")
public class LoginPage {

    @Autowired
    private UserInfoTestService us;

    @GetMapping({"", "index"})
    public String index(RedirectAttributes attrs) {
        if (StpUtil.isLogin()) {
            return "/index";
        } else {
//            attrs.addAttribute("msg", "Please Login");
//            attrs.addFlashAttribute("msg", "Please Login");
            return "redirect:/login";
        }
    }

    @GetMapping({"login"})
    public String login() {
        return "/login";
    }

    @RequestMapping("doLogin")
    public String doLogin(RedirectAttributes attrs, HttpSession session, @RequestParam String name, @RequestParam String password, @RequestParam String lastUrl) {
        log.info(name + " " + password);
        String msg;
        Optional<UserInfoTest> ui = us.getUser(name, password);
        log.info(ui.toString());
        if (ui.isPresent()) {
            int id = ui.get().getId();
            String username = ui.get().getName();
            StpUtil.login(id);
            SaSession saSession = StpUtil.getSession(true);
            saSession.set("userId", id);
            saSession.set("name", username);
            saSession.set("loginName", name);
            saSession.set("roleId", 1);
            session.setAttribute("userId", id);
            session.setAttribute("name", username);
            session.setAttribute("loginName", name);
            session.setAttribute("roleId", 1);
        } else {
            msg = "Failure: Username or password is wrong";
            attrs.addFlashAttribute("msg", msg);
            return "redirect:/login";
        }
        if (lastUrl != null) {
            return "redirect:" + lastUrl;
        }
        return "redirect:/index";
    }

    @GetMapping("doLogout")
    public String logout(HttpSession session) {
        StpUtil.logout();
        session.invalidate();
        return "redirect:/index";
    }

    @GetMapping("isLogin")
    public String isLogin(Model model) {
        boolean flag = StpUtil.isLogin();
        log.debug(String.valueOf(flag));
        model.addAttribute("flag", flag);
        return "/isLogin";
    }

    @RequestMapping("register")
    public String register() {
        return "redirect:/login";
    }

    @RequestMapping("doRegister")
    public String doRegister() {
        return "redirect:/login";
    }

}
